global g_root "C:\Users\Hongye Guo\Dropbox (Personal)\Density\Code\Production\Submission"
local l_date : display %tdCYND date(c(current_date), "DMY")
local l_time = substr(c(current_time), 1, 2) + substr(c(current_time), 4, 2)

* Start
use "${g_root}\Data\crsp_monthly_202106.dta", clear
gen ym = mofd(date)

tostring siccd, replace
replace siccd = "" if strlen(siccd) == 0
replace siccd = "0" + siccd if strlen(siccd) == 3
replace siccd = "" if siccd== "9999" | siccd== "9990" | siccd == "9910" | siccd == "0"
gen sic1 = substr(siccd, 1, 1)
gen sic2 = substr(siccd, 1, 2)
gen sic3 = substr(siccd, 1, 3)
rename siccd sic4 
destring sic1 sic2 sic3 sic4, replace 

tempfile tf_base
save `tf_base', replace

clear matrix
local col_count = 0
local title_list

foreach ii in sic2 sic3 sic4{
	use `tf_base', clear
	global g_ind `ii'
	
	* Minimum filters
	keep if inlist(shrcd, 10, 11, 12, 30, 31, 32)
	drop if inlist(secstat, "Q", "W") 

	* Additional filters
	replace prc = -prc if prc<0
	gen mkval = prc * shrout
	
	sort permno ym
	tsset permno ym
	gen mkval_l1m = L1.mkval
	gen ret_f1m = F1.ret
	drop if mi(mkval) | mi(ret_f1m) | mi(${g_ind})
	
	sort ym 
	by ym: egen temp = pctile(mkval) if primexch == "N", p(10)
	by ym: egen p10_mkval_nyse = max(temp)
	drop temp
	drop if mkval<p10_mkval_nyse 
	
	* Aggregate to the industry level
	gen num = mkval * ret_f1m
	foreach l_var of global g_ind {
		sort ym `l_var'
		by ym `l_var': egen temp_num = total(num)
		by ym `l_var': egen ym_`l_var'_mkval = total(mkval)
		gen ym_`l_var'_ret_f1m = temp_num/ym_`l_var'_mkval
		drop temp_num 
	}
		
	sort ym 
	by ym : egen temp_num = total(num)
	by ym : egen ym_mkval = total(mkval)
	gen ym_ret_f1m = temp_num/ym_mkval
	drop temp_num 
	
	sort ym ${g_ind}
	by ym ${g_ind}: gen ym_${g_ind}_count = _N
	by ym ${g_ind}: keep if _n==1
	
	* Industry variables
	gen ym_${g_ind}_exret_f1m = ym_${g_ind}_ret_f1m - ym_ret_f1m
	tsset ${g_ind} ym 
	forvalues i = 1/13{
		local im1 = `i' - 1
		gen ym_${g_ind}_exret_l`im1'm = L`i'.ym_${g_ind}_exret_f1m
	}

	sort ym ${g_ind} 
	by ym: egen temp_den = total(ym_${g_ind}_mkval)
	gen wgt = ym_${g_ind}_mkval/temp_den
	drop temp_den

	gen month = month(dofm(ym + 1))
	gen am_flag = mod(month, 3) == 1

	forvalues i = 1/4{
		local j = (`i'-1)*3 
		local jp1 = `j' + 1
		local jp2 = `j' + 2
		foreach l_var in ym_${g_ind}_exret {
			gen `l_var'_prevq_l`i'q = `l_var'_l`j'm if mod(month,3)==2
			replace `l_var'_prevq_l`i'q = `l_var'_l`jp1'm if mod(month,3)==0
			replace `l_var'_prevq_l`i'q = `l_var'_l`jp2'm if mod(month,3)==1
			gen `l_var'_prevq_l`i'q_xam = `l_var'_prevq_l`i'q * am_flag
		}
	}
	
	local lvarlist ym_${g_ind}_exret 
	foreach l_var in `lvarlist'{
		gen `l_var'_prevq_t4q = `l_var'_prevq_l1q + `l_var'_prevq_l2q + `l_var'_prevq_l3q + `l_var'_prevq_l4q 
		gen `l_var'_prevq_t4q_xam = `l_var'_prevq_t4q * am_flag 
	}
	
	* Table 9
	{ 
		reg ym_${g_ind}_exret_f1m ym_${g_ind}_exret_prevq_t4q [weight = wgt] if ym_${g_ind}_count>19, cluster(ym)
		local col_count = `col_count' + 1
		estimates store sep_`col_count'	
		local title_list `title_list' "All"	
		reg ym_${g_ind}_exret_f1m ym_${g_ind}_exret_prevq_t4q [weight = wgt] if am_flag & ym_${g_ind}_count>19, cluster(ym)
		local col_count = `col_count' + 1
		estimates store sep_`col_count'	
		local title_list `title_list' "AM"	
		reg ym_${g_ind}_exret_f1m ym_${g_ind}_exret_prevq_t4q [weight = wgt] if !am_flag & ym_${g_ind}_count>19, cluster(ym)
		local col_count = `col_count' + 1
		estimates store sep_`col_count'	
		local title_list `title_list' "Non-AM"	
		reg ym_${g_ind}_exret_f1m ym_${g_ind}_exret_prevq_t4q ym_${g_ind}_exret_prevq_t4q_xam am_flag [weight = wgt] if ym_${g_ind}_count>19, cluster(ym)
		local col_count = `col_count' + 1
		estimates store sep_`col_count'	
		local title_list `title_list' "Difference"	
		
	}
}

esttab sep_* using "$g_root/Output/USMomentum_sep_`l_date'`l_time'.csv", star(* 0.1  ** 0.05 *** 0.01) b(3) t(2) r2 br noomit replace order(`order_list') mtitles(`title_list')

tempfile tf_main
save `tf_main', replace

* Table A4
{
	use `tf_main', clear
	drop if ym_${g_ind}_count<=19

	gen pt_bin = .
	gen signal = ym_${g_ind}_exret_prevq_t4q
	replace signal = -ym_${g_ind}_exret_prevq_t4q if am_flag

	sort ym
	by ym: egen temp_num = total(signal * wgt)
	by ym: egen temp_den = total(wgt) if !mi(temp_num)
	gen signal_dm = signal - temp_num/temp_den
	drop temp_num temp_den


	sum ym if !mi(signal_dm) & !mi(ym_${g_ind}_exret_f1m) 
	local l_min = `r(min)' 
	local l_max = `r(max)' + 1 

	gen signal_dm_sd = .
	forvalues l_ym = `l_min'/`l_max'{
		sum signal_dm [weight = wgt] if ym <= `l_ym' 
		replace signal_dm_sd = `r(sd)' if ym == `l_ym' 
	}

	gen pwgt = signal_dm/signal_dm_sd 
	replace pwgt = 2 if pwgt >2 & !mi(pwgt)
	replace pwgt = -2 if pwgt <-2 & !mi(pwgt)
	replace pwgt = pwgt/20*100

	capture drop num
	gen one = 1
	sort ym  
	forvalues i = 1/1{
		gen num = pwgt * ym_${g_ind}_exret_f`i'm
		by ym: egen total_num = total(num) 
		gen ym_bin_ret_f`i'm = total_num
		drop total_num num 
	}

	drop if mi(ym_bin_ret_f1m)
	keep ym_bin_ret_f1m ym
	duplicates drop
	reg ym_bin_ret_f1m, robust
	count
	local l_count = `r(N)'+1
	set obs `l_count'
	replace ym = ym(2021,6) if mi(ym)
	merge 1:1 ym using "$g_root\Data\basic_factor_202106.dta", keep(1 3) nogen

	tsset ym
	foreach l_signal in mktrf hml smb mom {
		gen `l_signal'_f1m= F1.`l_signal'
	}
	sum mktrf_f1m 
	local l_sd_mkt = `r(sd)'
	sum ym_bin_ret_f1m 
	replace ym_bin_ret_f1m = ym_bin_ret_f1m/`r(sd)'*`l_sd_mkt'

	* Table A4
	clear matrix
	local col_count = 0

	reg ym_bin_ret_f1m if ym>=ym(1926,10) & ym<=ym(2021,5), robust
	local col_count = `col_count' + 1
	estimates store sep_`col_count'	
	reg ym_bin_ret_f1m mktrf_f1m if ym>=ym(1926,10) & ym<=ym(2021,5), robust
	local col_count = `col_count' + 1
	estimates store sep_`col_count'	
	reg ym_bin_ret_f1m mktrf_f1m hml_f1m smb_f1m if ym>=ym(1926,10) & ym<=ym(2021,5), robust
	local col_count = `col_count' + 1
	estimates store sep_`col_count'	
	reg ym_bin_ret_f1m mktrf_f1m hml_f1m smb_f1m mom_f1m if ym>=ym(1926,10) & ym<=ym(2021,5), robust
	local col_count = `col_count' + 1
	estimates store sep_`col_count'	

	esttab sep_* using "$g_root/Output/USalpha_`l_date'`l_time'.csv", star(* 0.1  ** 0.05 *** 0.01) b(3) t(2) r2 br noomit replace 
}

